<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="日期范围">
          <el-date-picker v-model="queryParams.dateRange" type="datetimerange" range-separator="至"
                          start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                          class="small-date-picker" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="countPvInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="渠道名称" align="center" prop="channelName" />
      <template v-if="type == 'open'">
        <el-table-column label="打开量" align="center" prop="gzhCount" />
<!--        <el-table-column label="公众号名称" align="center" prop="officialName" />-->
        <el-table-column label="企业微信打开量" align="center" prop="comCount" />
<!--        <el-table-column label="企业微信打开名称" align="center" prop="officialName" />-->
      </template>
      <template v-if="type == 'er'">
        <el-table-column label="长按量" align="center" prop="erCount" />
        <el-table-column label="企业微信长按量" align="center" prop="wxComCount" />
      </template>
      <el-table-column label="日期" align="center" width="150">
        <template slot-scope="scope">
          {{formatTimestamp(scope.row.dateInfo)}}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Count ref="CountRef" />
  </div>
</template>

<script>
import { listCountWxInfo } from "@/api/channel/countWxInfo";
import Count from "@/views/channel/countPvInfo/echars/count.vue";
import dayjs from "dayjs";

export default {
  name: "CountPvInfo",
  components: {Count},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道统计pv信息表格数据
      countPvInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      type: 'all',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        randomId: null,
        reqIp: null,
        browserName: null,
        platformName: null,
        dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        appCode: [
          { required: true, message: "应用编号不能为空", trigger: "blur" }
        ],
        appName: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        randomId: [
          { required: true, message: "pv随机ID不能为空", trigger: "blur" }
        ],
        reqIp: [
          { required: true, message: "pvip不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.$route.query
    }
    this.type = this.$route.query.type || 'all'
    this.getList();
  },
  watch:{
    $route(){
      this.getList()
    }
  },
  methods: {
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);

      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return dayjs(timestamp).format('YYYY-MM-DD');
    },
    /** 查询渠道统计pv信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.channelCode = this.$route.query.channelCode;
      // this.queryParams.dateInfo = this.$route.query.dateInfo;
      const sata = { ...this.queryParams };
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDateInfo = this.queryParams.dateRange[1];
        sata.startDateInfo = startDate;
        sata.endDateInfo = endDateInfo;
      }
      delete sata.dateRange;
      listCountWxInfo(sata).then(response => {
        this.countPvInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handle() {
      this.$refs.CountRef.show(this.queryParams)
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        randomId: null,
        reqIp: null,
        browserName: null,
        platformName: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('channel/countPvInfo/export', {
        ...this.queryParams
      }, `countPvInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
